package planit.controller.impl;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;

import planit.model.Model;
import planit.model.Person;
import planit.view.AddPersonToEventDialog;

@SuppressWarnings("serial")
public class AddPersonToEventController extends AbstractAction {
	private Model model;

	public AddPersonToEventController(Model model) {
		this.model = model;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		AddPersonToEventDialog dialog = 
			(AddPersonToEventDialog)this.getValue("AddPersonToEventDialog");

		boolean exists = true;
		model.getSchedule().getPerson(dialog.getPersonId());
		ArrayList<Person> persons = model.getSchedule().getEvent(
				dialog.getEventId()).getPersons();
		for(Person p:persons){
			if(p== model.getSchedule().getPerson(dialog.getPersonId())){
				exists=false;
			}
		}
		if(exists){
			model.getSchedule().getEvent(
					dialog.getEventId()).addPerson(
							model.getSchedule().getPerson(dialog.getPersonId()));
		}
		dialog.updateEventPersonsList();
	}
}
